from pyjenkinsci.jenkinsobject import jenkinsobject
from pyjenkinsci.exceptions import ArtifactBroken

import urllib2
import re

import logging

log = logging.getLogger( __name__ )

class fingerprint( jenkinsobject ):
    """
    Represents a hudson environment.
    """ 
    RE_MD5 = re.compile( "^([0-9a-z]{32})$" )
       
    def __init__( self, baseurl, id, hudson ):
        logging.basicConfig()
        self.hudson = hudson
        assert self.RE_MD5.search( id ), "%s does not look like a valid id" % id
        url =  "%s/fingerprint/%s/" % ( baseurl, id  )
        jenkinsobject.__init__( self, url, poll=False )    
        self.id = id
        
    def getHudsonObject(self):
        return self.hudson
        
    def __str__(self):
        return self.id
    
    def valid(self):
        """
        Return True / False if valid
        """
        try:
            self.poll()
        except urllib2.HTTPError, e:
            return False
        return True
    
    def validate_for_build(self, filename, job, build):
        if not self.valid():
            log.info("Unknown to Hudson.")
            return False
        
        if not self._data["original"] == None: 
            if self._data["original"]["name"] == job:
                if self._data["original"]["number"] == build:
                    return True

        if self._data["fileName"] != filename:
            log.info("Filename from Hudson (%s) did not match provided (%s)" % ( self._data["fileName"], filename ) )
            return False
            
        for usage_item in self._data["usage"]:
            if usage_item["name"] == job:
                for range in usage_item["ranges"]["ranges"]:
                    if range["start"] <= build <= range["end"]:
                        log.info("This artifact was generated by %s between build %i and %i" % ( job, range["start"],  range["end"] ) )
                        return True
        
        return False
    
    def validate(self):
        try:
            assert self.valid()
        except AssertionError, ae:
            raise ArtifactBroken( "Artifact %s seems to be broken, check %s" % ( self.id, self.baseurl ) )
        except urllib2.HTTPError, httpe:
            raise ArtifactBroken( "Unable to validate artifact id %s using %s" % ( self.id, self.self.baseurl ) )
        return True
    
    def getInfo( self ):
        """
        Returns a tuple of build-name, build# and artifiact filename for a good build.
        """
        self.poll()        
        return self._data["original"]["name"], self._data["original"]["number"], self._data["fileName"]

        
if __name__ == "__main__":
    ff = fingerprint( "http://localhost:8080/hudson/", "0f37cbb6545b8778bc0700d90be66bf3" )
    print repr(ff)
    print ff.baseurl
    print ff.valid()
    print ff.getInfo( )
